﻿using System;
using System.Xml;

namespace Moonlite.Services.CodeCompletion
{
    public static class GalaxyNativeCodeReader
    {
        private const string Path = "Moonlite.Services.CodeCompletion.Resources.Xml.GalaxyNativeCode.xml";

        public static GalaxyCodeObjectCollection Read()
        {
            GalaxyCodeObjectCollection objectCollection = new GalaxyCodeObjectCollection();
            XmlDocument document = new XmlDocument();
            document.Load(typeof(JassNativeCodeReader).Assembly.GetManifestResourceStream(Path));
            foreach (XmlNode node in document.ChildNodes)
            {
                if (node.Name.ToLower() == "galaxy")
                {
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        GalaxyCodeObject codeObject = new GalaxyCodeObject();
                        switch (childNode.Name.ToLower())
                        {
                            case "variable":
                                codeObject.Type = GalaxyCodeObjectType.Variable;
                                break;
                            case "native":
                                codeObject.Type = GalaxyCodeObjectType.Native;
                                break;
                            case "function":
                                codeObject.Type = GalaxyCodeObjectType.Function;
                                break;
                            default:
                                break;
                        }

                        foreach (XmlAttribute attribute in childNode.Attributes)
                        {
                            switch (attribute.Name.ToLower())
                            {
                                case "name":
                                    codeObject.Name = attribute.Value;
                                    break;
                                default:
                                    break;
                            }
                        }

                        codeObject.Code = childNode.InnerText;
                        objectCollection.Add(codeObject);
                    }
                }
            }

            return objectCollection;
        }
    }
}
